import 'package:amap_map_fluttify/amap_map_fluttify.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:amap_search_fluttify/amap_search_fluttify.dart';
import 'package:ytbusiness/utils/screen_adaptation.dart';

///
class SelectLocationFromMapPage extends StatefulWidget {
  @override
  _SelectLocationFromMapPageState createState() =>
      _SelectLocationFromMapPageState();
}

class _SelectLocationFromMapPageState extends State<SelectLocationFromMapPage> {
  AmapController _controller;
  List<Poi> poiList = [];
  String keyword = "";
  String address = "";
  bool isLoading = true;

  @override
  void initState() {
    super.initState();
    _initLocation();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomPadding: false, //防止底部布局被顶起
      appBar: AppBar(
        title: Text(
          '选择位置信息',
        ),
        elevation: 0.0,
        centerTitle: true,
      ),

      body: Column(
        children: <Widget>[
          Theme(
            data: ThemeData(
                primaryColor: Color(0xFFFFCA28), hintColor: Color(0xFFFFCA28)),
            child: Container(
              color: Color(0xFFFFCA28),
              padding: EdgeInsets.all(5),
              child: Container(
                height: 36,
                margin: EdgeInsets.only(left: 5, right: 5, bottom: 5),
                child: TextField(
                  style: TextStyle(fontSize: 16, letterSpacing: 1.0),
                  controller: TextEditingController.fromValue(TextEditingValue(
                    // 设置内容
                    text: keyword,
                    selection: TextSelection.fromPosition(TextPosition(
                        affinity: TextAffinity.downstream,
                        offset: keyword?.length ?? 0)),
                    // 保持光标在最后
                  )),
                  decoration: InputDecoration(
                    border: OutlineInputBorder(
                      borderRadius: BorderRadius.all(Radius.circular(40)),
                    ),
                    hintText: '输入关键字',
                    hintStyle:
                        TextStyle(color: Color(0xFFBEBEBE), fontSize: 14),
                    contentPadding:
                        const EdgeInsets.symmetric(vertical: 8, horizontal: 1),
                    prefixIcon: Icon(
                      Icons.search,
                      color: Colors.grey,
                      size: 20,
                    ),
                    suffixIcon: IconButton(
                      icon: Icon(
                        Icons.clear,
                        color: Colors.grey,
                        size: 20,
                      ),
                      onPressed: () {
                        keyword = "";
                        setState(() {});
                      },
                    ),
                    fillColor: Colors.white,
                    filled: true,
                  ),

                  inputFormatters: [],
                  //内容改变的回调
                  onChanged: (text) {
                    print('change $text');
                    keyword = text;
                  },
                  //内容提交(按回车)的回调
                  onSubmitted: (text) async {
                    poiList = await AmapSearch.instance
                        .searchKeyword(text, city: text);
                    setState(() {});
                    print(poiList.length);
                  },
                  //按回车时调用
                  onEditingComplete: () {},
                ),
              ),
            ),
          ),
          Container(
            height: 300,
            child: Stack(
              children: <Widget>[
                AmapView(
                  showZoomControl: false,
                  centerCoordinate: LatLng(39, 110),
                  maskDelay: Duration(milliseconds: 500),
                  zoomLevel: 16,
                  onMapCreated: (controller) async {},
                  onMapMoveEnd: (MapMove move) async {},
                ),
                Center(
                  child: Icon(
                    Icons.place,
                    size: 36.0,
                    color: Color(0xFFFF0000),
                  ),
                ),
              ],
            ),
          ),
          Expanded(
              child: Container(
            child: ListView.builder(
                itemCount: poiList.length,
                itemBuilder: (context, index) {
                  return Container(
                    child: Row(
                      children: [
                        Container(
                          child: Text(poiList[index].title),
                          margin: EdgeInsets.only(right: setWidth(20)),
                        ),
                      ],
                    ),
                  );
                }),
          ))
        ],
      ),
    );
  }

  _initLocation() async {
    await AmapCore.init('92981bb66e34604acef543903456ea4f');
    await _controller.setCenterCoordinate(LatLng(116.428128, 39.912057),
        animated: false);
  }
}
